<template>
  <div>
    <div
        style="width: 400px;height: 350px;margin: 150px auto;background-color: rgba(130,109,224,0.5);border-radius: 10px">
      <div style="width: 100%;height: 100px;font-size: 30px;line-height: 100px;text-align: center;color: #5c4ad0">
        欢迎注册
      </div>
      <div style="margin-top: 25px;text-align: center;height: 320px">
        <el-form :model="user">
          <el-form-item>
            <el-input v-model="user.userName" prefix-icon="el-icon-user" style="width: 80%"
                      placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input v-model="user.password" prefix-icon="el-icon-lock" style="width: 80%"
                      placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button style="width: 80%;margin-top:10px" type="primary" @click="register()">注册</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import axiosUtil from "@/utils/axiosUtil";

export default {
  name: 'registerView',
  data() {
    return {
      user: {
        userName: null,
        password: null
      }
    }
  },
  methods: {
    register() {
      axiosUtil.post("/user/register", this.user).then(res => {
        if (res['code'] === '1') {
          this.$message.success('注册成功');
          // 跳转到登录页面
          this.$router.push("/login");
        } else {
          this.$message.error(res['msg']);
        }
      }).catch(error => {
        console.error('网络错误:', error);
        this.$message.error('网络错误，请稍后重试');
      });
    }
  }
}
</script>
